package com.efast.cafe.portal.controller.api;



import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.controller.AbstractController;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.vo.DataTableVo;
import com.efast.cafe.portal.bean.api.ApiModuleDefineBean;
import com.efast.cafe.portal.bean.api.ApiModuleDefineQueryBean;
import com.efast.cafe.portal.entity.api.ApiModuleDefine;
import com.efast.cafe.portal.entity.api.ApiSystemModuleDef;
import com.efast.cafe.portal.service.api.IApiModuleDefineService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.support.RequestContext;

import net.sf.json.JSONObject;



@Controller
@RequestMapping("/frame_center/api")
public class ApiModuleDefineController extends AbstractController{

	@Autowired
	private IApiModuleDefineService  apiModuleDefineService;
	
	

	@RequestMapping(value = "/module/page")
	public String templatePage(@RequestParam(required = false) String searchValue, ModelMap model,
			HttpServletRequest request, HttpServletResponse response){
		
		return "/api/module/list";

	}
	/**
	 * 查询module
	 * @Date        :      2017年1月18日
	 * @param queryBean
	 * @return
	 */
	@RequestMapping(value = "/module/list")
	public @ResponseBody DataTableVo<ApiModuleDefine> queryPageList(
			@ModelAttribute ApiModuleDefineQueryBean queryBean){
		return outPageJSONArray_bootstrapDatatable(apiModuleDefineService.querylistPosition(queryBean));
	}
	
	
	
	@RequestMapping(value = "/module/detail")
	public String formPage(Integer systemId,Integer moduleId,
			HttpServletRequest request, 
			HttpServletResponse response,ModelMap model){
		ApiModuleDefineBean apiModuleDefineQueryBean = null;
		if(moduleId!=null){
			apiModuleDefineQueryBean= apiModuleDefineService.queryApiModuleDefineById(systemId,moduleId);
		}else{
			apiModuleDefineQueryBean = new ApiModuleDefineBean();
		}
		JSONObject ajaxsJson = JSONObject.fromObject(apiModuleDefineQueryBean);
		model.addAttribute("apiModuleDefineQueryBean", ajaxsJson);
		return "/api/module/detail";
	}
	
	/**
	 * 新增模块
	 * @Date        :      2017年1月18日
	 * @param moduleDefineInsertBean
	 * @param request
	 * @return
	 */
	//@RequestMapping(value = "/module/add")
	public @ResponseBody ResultBean saveApiModuledefine(
			@ModelAttribute ApiModuleDefineBean moduleDefineInsertBean,
			HttpServletRequest request){
		RequestContext context=new RequestContext(request);
		ResultBean result = new ResultBean(true, context.getMessage(ADD_SUCCESS_TIP));
		Integer id = null;
		try{
			id = apiModuleDefineService.saveApiModuledefine(moduleDefineInsertBean);
			result.setObject(id.toString());
		}catch(Exception e){
			result.setSuccess(false);
			if(e instanceof ServiceException){
				result.setMessage(context.getMessage(e.getMessage()));
			}else{
				result.setMessage(context.getMessage(ADD_FAILURE_TIP));
			}
		}
		return result;
	}
	
	
	/**
	 * 查看单个模块信息
	 * @Date        :      2017年1月18日
	 * @param id
	 * @param systemVersion
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	/*@RequestMapping(value="/module/detail")
	public @ResponseBody ApiModuleDefineInsertBean queryApiModuleDefineById(Integer id,String systemVersion,HttpServletRequest request, 
			HttpServletResponse response,ModelMap model) {
			
		ApiModuleDefineInsertBean apiModuleDefineInsertBean= apiModuleDefineService.queryApiModuleDefineById(id, systemVersion);
			
		return apiModuleDefineInsertBean;
	}
	*/
	//@RequestMapping(value = "/module/update")
	public @ResponseBody ResultBean updateModuleDefine(
			@ModelAttribute ApiModuleDefineBean moduleDefineInsertBean,
			HttpServletRequest request){
		RequestContext context=new RequestContext(request);
		ResultBean result = new ResultBean(true, context.getMessage(EDIT_SUCCESS_TIP));
		try {
			moduleDefineInsertBean.setIsDel((byte) 0);
			//调用修改方法
			apiModuleDefineService.updateModuleDefine(moduleDefineInsertBean);
		} catch (Exception e) {
			result.setSuccess(false);
			if(e instanceof ServiceException){
				result.setMessage(context.getMessage(e.getMessage()));
			}else{
				result.setMessage(context.getMessage(EDIT_FAILURE_TIP));
			}
		}
		return result;
	}
	
	
	/**
	 * 通过systemId查询system的所有模块
	 * @Date        :      2017年1月20日
	 * @param queryBean
	 * @return
	 */
	@RequestMapping(value = "/module/querylistModuleBySystemId")
	public @ResponseBody List<ApiModuleDefineBean> querylistModuleBySystemId(
			@ModelAttribute Integer systemId){
		return apiModuleDefineService.querylistModuleBySystemId(systemId);
	}
	
	
	/**
	 * 跳转到模块重用页面
	 * @Date        :      2017年2月7日
	 * @param moduleId
	 * @param systemVersion
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/module/reuseForm")
	public String reuseForm(Integer systemId,Integer moduleId ,
			HttpServletRequest request, 
			HttpServletResponse response,ModelMap model){
		ApiModuleDefineBean apiModuleDefineQueryBean = apiModuleDefineService.queryApiModuleDefineById(systemId,moduleId);
		JSONObject ajaxsJson = JSONObject.fromObject(apiModuleDefineQueryBean);
		model.addAttribute("apiModuleDefineQueryBean", ajaxsJson);
		return "/api/module/reuseForm";
	}
	
	/**
	 * 模块重用
	 * @Date        :      2017年2月7日
	 * @param moduleDefineInsertBean
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/module/reuse")
	public @ResponseBody ResultBean reuseApiModuledefine(
			@ModelAttribute ApiSystemModuleDef apiSystemModuleDef,HttpServletRequest request){
		RequestContext context=new RequestContext(request);
		ResultBean result = new ResultBean(true, context.getMessage(ADD_SUCCESS_TIP));
		Integer id = null;
		try{
			id = apiModuleDefineService.reuseModule(apiSystemModuleDef);
			result.setObject(id.toString());
		}catch(Exception e){
			result.setSuccess(false);
			if(e instanceof ServiceException){
				result.setMessage(context.getMessage(e.getMessage()));
			}else{
				result.setMessage(context.getMessage(ADD_FAILURE_TIP));
			}
		}
		return result;
	}
	
}
